package com.huawei.spare.part.replace.models;

import com.google.common.collect.Lists;
import com.huawei.spare.part.replace.models.constants.Plane;
import com.huawei.spare.part.replace.utils.Application;
import lombok.Data;
import lombok.ToString;

import java.io.File;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * project model represent all attributes required by a "replacement" project
 */
@Data
@ToString
public class Project implements Serializable {

    String projectId;

    DbMeta dbMeta = new DbMeta();

    Spare spare;
    Defective defective;

    Switch switch0;
    Solution solution;

    File saveTo;


//    InstallPackages installPackages = new InstallPackages();

    /**
     * 本地已下载安装包
     */
    DownloadPackages downloadPackages = new DownloadPackages();

    public Project() {
        projectId = new SimpleDateFormat("yyyyMMddHHmmss_SSS").format(new Date());
    }

    public File home() {
        return Application.getProject(projectId);
    }

    @Data
    @ToString
    public static class InstallPackages implements Serializable {
        InstallPackage fabric;
        InstallPackage base;
        InstallPackage cpld;
        InstallPackage bmc;
    }

    /**
     * 本地已下载安装包
     */
    @Data
    @ToString
    public static class DownloadPackages implements Serializable {
        DownloadPackage fabric;
        DownloadPackage base;
        DownloadPackage cpld;
        DownloadPackage bmc;

        public List<String> getAllPackageNames() {
            final ArrayList<String> result = Lists.newArrayList();
            if (fabric != null) {
                result.add(fabric.getPackageName());
            }
            if (base != null) {
                result.add(base.getPackageName());
            }
            if (cpld != null) {
                result.add(cpld.getPackageName());
            }
            if (bmc != null) {
                result.add(bmc.getPackageName());
            }
            return result.stream().distinct().collect(Collectors.toList());
        }
    }


    @Data
    @ToString
    public static class Defective implements Serializable {
        String boardType;
        String softwareVersion;
        String cpldVersion;
        Boolean isStacked;
        Boolean isV5;
        Boolean ignoreCX910Base;
        SwitchMatchedPackage matchedPackage;
    }

    @Data
    @ToString
    public static class Spare implements Serializable {
        String sn;
        String boardType;
        String softwareVersion;
        String cpldVersion;
        Boolean isV5;
        SwitchMatchedPackage matchedPackage;
    }


    /**
     * 是否需要升级 Fabric 平面
     *
     * @return
     */
    public boolean isFabricUpgradeRequired() {
        return switch0.getPlanes().contains(Plane.Fabric) && solution.getFabric().getUpgrade();
    }

    /**
     * 是否需要升级 Base 平面
     *
     * @return
     */
    public boolean isBaseUpgradeRequired() {
        return switch0.getPlanes().contains(Plane.Base) && solution.getBase().getUpgrade() && !defective.getIgnoreCX910Base();
    }

}
